/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.platform.modules.sys.service.impl;

import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.common.web.utils.Pager;
import com.platform.modules.sys.dao.SysRoleDao;
import com.platform.modules.sys.dao.SysRoleMenuDao;
import com.platform.modules.sys.domain.SysRole;
import com.platform.modules.sys.domain.SysRoleMenu;
import com.platform.modules.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 系统-角色表 服务层实现
 * </p>
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {

    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(sysRoleDao);
    }

    @Override
    public List<SysRole> queryList(SysRole t) {
        List<SysRole> dataList = sysRoleDao.queryList(t);
        return dataList;
    }

    @Override
    public Pager<SysRole> queryPager(SysRole t) {
        List<SysRole> dataList = queryList(t);
        return formatPager(dataList);
    }

    @Override
    public Integer uniqueName(SysRole sysRole) {
        return sysRoleDao.uniqueName(sysRole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addRole(SysRole sysRole) {
        // 新增角色
        super.add(sysRole);
        // 新增角色菜单
        return batchSaveRoleMenu(sysRole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateRole(SysRole sysRole) {
        // 更新角色
        super.updateById(sysRole);
        // 删除角色菜单
        sysRoleMenuDao.deleteByRoleId(sysRole.getId());
        // 新增角色菜单
        return batchSaveRoleMenu(sysRole);
    }

    /**
     * 批量新增角色和菜单关联
     */
    private boolean batchSaveRoleMenu(SysRole sysRole) {
        List<Long> menuIds = sysRole.getMenuIds();
        if (CollectionUtils.isEmpty(menuIds)) {
            return true;
        }
        Long roleId = sysRole.getId();
        List<SysRoleMenu> roleMenus = new ArrayList<>();
        for (Long menuId : menuIds) {
            if (menuId != null) {
                roleMenus.add(new SysRoleMenu(roleId, menuId));
            }
        }
        if (!CollectionUtils.isEmpty(roleMenus)) {
            sysRoleMenuDao.batchSave(roleMenus);
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteRole(Long roleId) {
        // 删除角色
        super.deleteById(roleId);
        // 删除角色菜单
        sysRoleMenuDao.deleteByRoleId(roleId);
        return true;
    }

}
